﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Threading;

namespace PRO_DOCS.BLL.WIN.Authentication
{
    public partial class SigningIn : Templates.DevxMasterTemplate
    {
        public SigningIn()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            dxErrorProvider1.ClearErrors();

            InsertionAccepteed = !String.IsNullOrEmpty(txtUserName.Text) &&
                                 !String.IsNullOrEmpty(txtPassword.Text);

            if (InsertionAccepteed)
            {
                if (txtUserName.Text.Equals("dbadmin") && txtPassword.Text.Equals("dbadmin"))
                {
                    if (new ConnectionStrings().UPDATE_CONNECTION_STRING())
                    {
                        Application.Restart();
                    }
                }
                else
                {
                    Authentication.LOGGED_IN_EMPLOYEE_DETAILS.SignInUser(txtUserName.Text, txtPassword.Text);
                    if (Authentication.LOGGED_IN_EMPLOYEE_DETAILS.IS_AUTHENTICATED_USER)
                    {
                        Properties.Settings.Default.LastUsedUserName = txtUserName.Text;
                        Properties.Settings.Default.Save();
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show(Properties.Messages.Default.WrongLoginInfo.Replace("{nl}", Environment.NewLine),
                                        "ECHO®-eDocument Management System",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Information);
                    }
                }
            }
            else
            {
                string errMsg = String.Empty;
                errMsg = (String.IsNullOrEmpty(txtUserName.Text)) ? Properties.ErrorProviderMessages.Default.InputLeftBlank.Replace("{name}", "User name") : String.Empty;
                dxErrorProvider1.SetError(txtUserName, errMsg);

                errMsg = (String.IsNullOrEmpty(txtPassword.Text)) ? Properties.ErrorProviderMessages.Default.InputLeftBlank.Replace("{name}", "Password") : String.Empty;
                dxErrorProvider1.SetError(txtPassword, errMsg);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SigningIn_Load(object sender, EventArgs e)
        {
            txtUserName.Text = Properties.Settings.Default.LastUsedUserName;
        }
    }
}